#ifndef TRACKSMANAGER_H
#define TRACKSMANAGER_H

#include <QtGui>
#include "gpsmanager.h"

#include "controls/rotatingwidget.h"

class StatsTracks;
class BreadcrumbsSync;
class FTPushButton;
class FTWidgetList;
class GPS_track;
class GPS_leg;
class FTComboBox;
class FTRadioButton;
class FTLabel;
class FTLineEdit;
class FTCheckBox;
class FTColorDialog;

class TracksManager : public RotatingWidget
{
    Q_OBJECT
public:
    explicit TracksManager(const GpsManager*);
    ~TracksManager();

    // Return a list of the tracks
    QList<int> listTracks() const;
    // Get a apecific track
    const GPS_track* trackPtr(int) const;

signals:
    // Emitted when requested to navigate to a point of a track
    void navigateTo( QGeoCoordinate );
    // Emitted when track navigation is stopped (due to error or starting a new navigation)
    void navigationStopped();

public slots:
    void destinationReached();
    void stopNavigation();

private slots:
    void loadTrack();
    void importTrack();
    void sortTracks(QList<int>);
    void removeTrack(int);

    void openEdit(int);
    void closeEdit();

    void openBreadcrumbs();
    void closeBreadcrumbs();

    void colorsSelectLeg();
    void colorsModifyLeg();
    void closeColors();
    void openColors();

    void openNavigation();
    void startNavigation();
    void closeNavigation();

    void trackImported();


private:
    void buildMain();
    void buildEdit();
    void buildBreadcrumbs();
    void buildColors();
    void buildnavigation();
    void addTrack(const QString&);
    void addTrack(GPS_track*);

    FTPushButton* load_track_button;
    FTPushButton* import_track_button;

    QHash<int, GPS_track*> tracks;

    // Main page
    QWidget* mainPage;
    QStackedWidget* pages;
    FTWidgetList* track_list_widget;

    // Edit page
    QWidget* editPage;
    StatsTracks* stats;
    FTPushButton* navigationButton;
    FTPushButton* breadcrumbsButton;
    FTPushButton* colorsButton;
    FTPushButton* closeEditButton;

    // Breadcrumbs page
    QWidget* breadcrumbsPage;
    BreadcrumbsSync* breadcrumbs;
    FTPushButton* breadcrumbsCloseButton;

    // Colors page
    QWidget* colorsPage;
    FTComboBox* colorsList;
    FTColorDialog* colorDialog;
    FTPushButton* closeColorsButton;

    // navigation page
    QWidget* navigationPage;
    FTLabel* navigationLegLabel;
    FTComboBox* navigationLegList;
    FTRadioButton* navigationBeginningRadio;
    FTRadioButton* navigationClosestRadio;
    FTLabel* navigationProximityLabel;
    FTLineEdit* navigationProximityEdit;
    FTPushButton* startnavigationButton;
    FTPushButton* closenavigationButton;
    FTCheckBox* navigationReverseButton;
    QButtonGroup navigationMode;


    int selectedTrack;
    int selectedLeg;

    const GPS_track* getSelectedTrack();
    const GPS_leg* getSelectedLeg();

    QLayout* createHorizontal();
    QLayout* createVertical();

private: // navigation mode stuff
    bool isNavigating;
    int navigationTrack;
    int navigationLeg;
    int navigationPoint;
    bool navigationReverse;
    const GpsManager* gpsManager;

};

#endif // TRACKSMANAGER_H
